/*
 * Copyright (C) 2012 xjuraj
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.websearch.document.preprocessing.actions;

import java.io.IOException;
import java.io.Serializable;
import org.apache.log4j.Logger;
import org.apache.xmlbeans.impl.common.Levenshtein;
import sk.lieskove.jianghongtiao.common.utils.FileUtils;
import sk.lieskove.jianghongtiao.common.utils.PropertiesUtils;
import sk.lieskove.jianghongtiao.websearch.service.ExcludeReasonEnum;
import sk.lieskove.jianghongtiao.websearch.persistence.WebSearchResponse;

/**
 * 
 * @author xjuraj e-mail: jjurco.sk_gmail.com
 */
public class ExcludeWithoutSearchQuery implements Serializable, PreprocessAction {
    
    private transient Logger log = Logger.getLogger(ExcludeWithoutSearchQuery.class);
    private transient PropertiesUtils pu = new PropertiesUtils(ExcludeWithoutSearchQuery.class);
    
    private final String UNI_DEG = "AKAD[\\. ]+ARCH\\.?|AKAD[\\. ]+MAL\\.?|"
            + "AKAD[\\. ]+SOCH\\.?|ARTD\\.?|AS\\.?|B[\\. ]+SC\\.?|"
            + "BA *\\(HONS\\)|BBA\\.?|BBS\\.?|BC\\.?|BC[\\. ]+A\\.?|"
            + "BD\\.?|CENG\\.?|CERTHE\\.?|CERTMGMT\\.?|CSC\\.?|DBA\\.?|"
            + "DIP[\\. ]+SURV\\.?|DIS\\.?|DOC\\.?|DR\\.?|DR[\\. ]+MONT\\.?|"
            + "DR[\\. ]+H[\\. ]+C\\.?|DRSC\\.?|DSC\\.?|DVM\\.?|EMBA\\.?|"
            + "ICDR\\.?|ICLIC\\.?|ING-PAED IGIP\\.?|ING\\.?|ING[\\. ]+ARCH\\.?|"
            + "IWE\\.?|J[\\. ]+D\\.?|JUC\\.?|JUDR\\.?|LL[\\. ]+M\\.?|"
            + "M[\\. ]+PHIL\\.?|MBA\\.?|MBC\\.?|MDC\\.?|MDDR\\.?|MGA\\.?|"
            + "MGR\\.?|MGR[\\. ]+ART\\.?|MPA\\.?|MPH\\.?|MSC\\.?|MSDR\\.?|"
            + "MUC\\.?|MUC\\.?|MUDR\\.?|MVDR\\.?|PAEDDR\\.?|PH[\\. ]+D\\.?|"
            + "RM[\\. ]*CM[\\. ]*DR\\.?|PHD\\.?|PHDR\\.?|PHMR\\.?|PROF\\.?|"
            + "RCDR\\.?|RMCMDR\\.?|RNDR\\.?|RSDR\\.?|RTDR\\.?|STL\\.?|"
            + "TH[\\. ]+D\\.?|THDR\\.?|THLIC\\.?|THMGR\\.?";

    @Override
    public WebSearchResponse preprocess(WebSearchResponse response) {
        log.debug("Try to extract HTML text. Html2Txt");
        if(response.getFile() == null){
            return response;
        }
        String fullName = response.getSearchQuery().toUpperCase().replaceAll(UNI_DEG, "").trim();
        String fileString = "";
        try {
            fileString = FileUtils.read(response.getFile(), "UTF-8").toUpperCase();
        } catch (IOException ex) {
            log.error("This never happen. UTF-8 is not supported!");
        }
        String[] split = fullName.split(" ");
        String surname = split[1];
        for (String wordString : fileString.split(" ")) {
            int dist = Levenshtein.distance(wordString, surname);
            if(dist < 3){
                response.setLevenshteinDistance(dist);
                response.setLevenshteinString(wordString);
                return response;
            }
            if(dist < 6){
                response.setLevenshteinDistance(dist);
                response.setLevenshteinString(wordString);
                log.info("Levenshtein distance was: "+dist+" for strings: '"+
                        wordString+"' & '"+surname+"'");
            }
        }
        response.setExcludeReasonEnum(ExcludeReasonEnum.SURNAME_NOT_PRESENT);
        return response;
    }
}
